package com.xqx.rabbitmq.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author：xingquanxiang createTime：2019/11/28 10:20
 * description:
 */
public class ConnectionUtil {
    /**
     * 获取 MQ连接
     * @return
     * @throws IOException
     * @throws TimeoutException
     */
    public static Connection getConnection() throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //主机地址；默认为localhost
        connectionFactory.setHost("localhost");
        //连接端口；默认为 5672
        connectionFactory.setPort(5672);
        //虚拟主机名称：默认为 /
        connectionFactory.setVirtualHost("/xqx");
        //连接用户名： 默认为 guest
        connectionFactory.setUsername("guest");
        //连接密码；默认为 guest
        connectionFactory.setPassword("guest");
        //创建连接
        return connectionFactory.newConnection();
    }
}
